const { defineConfig } = require("@vue/cli-service");
const { list } = require("./src/mock");
const bodyParser = require("body-parser");
const Mock = require("mockjs");
const userList = [
  {
    username: "admin",
    password: "admin",
    nickname: "管理员",
    avator: `https://robohash.org/${Mock.Random.id()}?set=set`,
  },
  {
    username: "13111111111",
    password: "123456",
    nickname: "甜蜜蜜",
    avator: `https://robohash.org/${Mock.Random.id()}?set=set4`,
  },
  {
    username: "123456@qq.com",
    password: "321321",
    nickname: "再不疯狂",
    avator: `https://robohash.org/${Mock.Random.id()}?set=set4`,
  },
];
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares(middle, { app }) {
      app.use(bodyParser.json());
      app.get("/api/list", (req, res) => {
        const { pageCode = 1, pageSize = 10 } = req.query;

        if (pageCode * pageSize <= list.length) {
          res.send({
            code: 200,
            data: list.slice((pageCode - 1) * pageSize, pageSize * pageCode),
          });
        } else {
          res.send({
            code: 201,
            data: null,
            message: "没有更多数据了！！！",
          });
        }
      });

      app.post("/api/detail", (req, res) => {
        const { id } = req.body;
        const item = list.find((v) => v.id === id);
        res.send({
          code: 200,
          data: item,
        });
      });

      app.post("/api/login", (req, res) => {
        const { username, password } = req.body;
        const user = userList.find(
          (v) => v.username === username && v.password === password
        );

        if (user) {
          res.send({
            code: 200,
            message: "登录成功， 欢迎" + user.nickname,
            data: {
              nickname: user.nickname,
              avator: user.avator,
            }
          });
        } else {
          res.send({
            code: 403,
            message: '账户密码错误， 请重新登录'
          })
        }
      });
      return middle;
    },
  },
});
